import {post, get, dele, put} from '@/utils/serviceHelper.js';

const YEAR_METER_LIST = "YEAR_METER_LIST";
const MONTH_METER_LIST = "MONTH_METER_LIST";
const DAY_METER_LIST = "DAY_METER_LIST";
const state = {
    yearMeterList: [],
    monthMeterList: [],
    dayMeterList: [],
}

const getters = {
    yearMeterList(state) {
        return state.yearMeterList;
    },
    monthMeterList(state) {
        return state.monthMeterList;
    },
    dayMeterList(state) {
        return state.dayMeterList;
    }
}

const actions = {

    reportForYear({commit, state}, param) {
        return new Promise((resolve, reject) => {
            post('/report/year', param).then((res) => {

                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    reportThosForYear({commit, state}, param) {
        return new Promise((resolve, reject) => {
            post('/report/thosYear', param).then((res) => {

                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    reportForMonth({commit, state}, param) {
        return new Promise((resolve, reject) => {
            post('/report/month', param).then((res) => {

                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    reportThosForMonth({commit, state}, param) {
        return new Promise((resolve, reject) => {
            post('/report/thosMonth', param).then((res) => {

                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    reportForDay({commit, state}, param) {
        return new Promise((resolve, reject) => {
            post('/report/day', param).then((res) => {

                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    reportThosForDay({commit, state}, param) {
        return new Promise((resolve, reject) => {
            post('/report/thosDay', param).then((res) => {

                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    getYearMeterList({commit, state}, param) {
        return new Promise((resolve, reject) => {
            get('/meter/year', param).then((res) => {
                if (res.code == 0) {
                    var array = [];
                    res.rs.forEach(item => {
                        var arr = JSON.stringify([item.month.toString().substring(0, 4) + "-" + item.month.toString().substring(4, 6), item.startScore, item.endScore, item.minScore, item.maxScore, item.score]);
                        array.push(JSON.parse(arr));
                    });
                    resolve(JSON.parse(JSON.stringify(array)));
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    getMonthMeterList({commit, state}, param) {
        return new Promise((resolve, reject) => {
            get('/meter/month', param).then((res) => {
                if (res.code == 0) {

                    var array = [];
                    res.rs.forEach(item => {

                        var arr = JSON.stringify([item.month.toString().substring(0, 4) + "-" + item.month.toString().substring(4, 6) + "-" + item.day.toString(), item.startScore, item.endScore, item.minScore, item.maxScore, item.score]);
                        array.push(JSON.parse(arr));
                    });
                    resolve(JSON.parse(JSON.stringify(array)));
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    getDayMeterList({commit, state}, param) {
        return new Promise((resolve, reject) => {
            get('/meter/day', param).then((res) => {
                if (res.code == 0) {
                    let optionData={
                        xData:[],
                        yData:[]
                    }
                    res.rs.forEach(item => {
                        let hourValue=''

                        optionData.xData.push(item.hour.toString().padStart(2,'0') + ":" + item.minute.toString().padStart(2,'0') + ":00");
                        optionData.yData.push(item.score);
                    });
                    resolve(JSON.parse(JSON.stringify(optionData)));
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    getRiskMeterList({commit, state}, param) {
        return new Promise((resolve, reject) => {
            get('/meter/risk', param).then((res) => {
                if (res.code == 0) {
                    resolve(res.rs);
                } else {
                    reject(res.messages);
                }
            })
        });
    }
}
const mutations = {
    [YEAR_METER_LIST](state, data) {

        state.yearMeterList = data.rs;
    },
    [MONTH_METER_LIST](state, data) {
        state.monthMeterList = data.rs;
    },
    [DAY_METER_LIST](state, data) {
        state.dayMeterList = data.rs;
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
